package com.shelpe.services.sinbad.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.jpa.AdgroupLog;
import com.shelpe.services.sinbad.entity.jpa.vo.AdgroupVo;
import com.shelpe.services.sinbad.entity.jpa.vo.ConvertKeywordVo;
import com.shelpe.services.sinbad.entity.jpa.vo.KeywordsVo;
import com.shelpe.services.sinbad.entity.jpa.vo.PlatformVo;
import com.shelpe.services.sinbad.repository.jpa.AdgroupLogRepository;
import com.shelpe.services.sinbad.repository.jpa.AdgroupRepository;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class PlatformService {

	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private AdgroupRepository adgroupRepository;
	@Autowired
	private AdgroupLogRepository adgroupLogRepository;

	public PlatformVo unserialize(String platform) { // 反序列化，对象到字符串的过程
		try {
			return objectMapper.readerFor(PlatformVo.class).readValue(platform);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String serialize(PlatformVo platform) { // 序列化，字符串到对象的过程 跟数据库交互
		try {
			return objectMapper.writeValueAsString(platform);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return "";
	}

	public KeywordsVo.Vo unserializeKeyword(String oldNote) { // 反序列化，对象到字符串的过程
		try {
			if (oldNote != null) {
				return objectMapper.readerFor(KeywordsVo.Vo.class).readValue(oldNote);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String serializeKeyword(ConvertKeywordVo note) { // 序列化，字符串到对象的过程
															// 跟数据库交互
		try {
			return objectMapper.writeValueAsString(note);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return "";
	}

	public void updateAdgroupMobileDiscount(long campaignID, int convertDiscount, int newDiscount) {
		List<Adgroup> adgroups = this.adgroupRepository.findByCampaignID(campaignID);
		List<Adgroup> adgroupDatas = new ArrayList<Adgroup>();
		// List<AdgroupLog> adgroupLogs = new ArrayList<AdgroupLog>();

		if (adgroups != null) {
			for (Adgroup adgroup : adgroups) {
				if (adgroup.getMobileDiscount() == convertDiscount) {
					adgroup.setMobileDiscount(newDiscount);
					adgroup.setUpdatedTime(new Date());

					// 溢价的操作记录
					// AdgroupVo adgroupVo = new AdgroupVo();
					// adgroupVo.setOldMobileDiscount(ConvertDiscount);
					// adgroupVo.setNewMobileDiscount(newDiscount);
					//
					// AdgroupLog adgroupLog = new AdgroupLog();
					// BeanUtils.copyProperties(adgroup, adgroupLog);
					// adgroupLog.setAdgroupID(adgroup.getAutoID());
					// adgroupLog.setCampaignID(adgroup.getCampaignID());
					// adgroupLog.setUpdatedTime(new Date());
					// adgroupLog.setAction(AdgroupLog.Action.MANUAL_UPDATE_MOBILEDISCOUNT);
					// try {
					// adgroupLog.setNote(this.objectMapper.writer().writeValueAsString(adgroupVo));
					// } catch (JsonProcessingException e) {
					// log.error("JsonProcessingException: {}", e);
					// adgroupLog.setNote("");
					// }

					adgroupDatas.add(adgroup);
					// adgroupLogs.add(adgroupLog);
				}
			}
			this.adgroupRepository.save(adgroupDatas);
			log.info("check adgroupDatas_size:{}", adgroupDatas.size());
			// this.adgroupLogRepository.save(adgroupLogs);
		}
	}
}
